function formatUrl(val) {
	url = val ? val : $('#seo_url').val();
	if (url != '') {
		url = url.replace(/[^a-z0-9\-\ ]/gi, '', url);
		url = $.trim(url);
		url = url.replace(/\s+/g, '-', url);
		url = url.replace(/\-+/g, '-', url);
		$('#seo_url').val(url.toLowerCase());
	}
}

function image_upload(field, thumb, token) {
	$('#dialog').remove();
	
	$('#content').prepend('<div id="dialog" style="padding: 3px 0px 0px 0px;"><iframe src="index.php?route=common/filemanager&token=' + token + '&field=' + encodeURIComponent(field) + '" style="padding:0; margin: 0; display: block; width: 100%; height: 100%;" frameborder="no" scrolling="auto"></iframe></div>');
	
	$('#dialog').dialog({
		title: 'Image Manager',
		close: function (event, ui) {
			if ($('#' + field).attr('value')) {
				$.ajax({
					url: 'index.php?route=common/filemanager/image&token=' + token + '&image=' + encodeURIComponent($('#' + field).attr('value')),
					dataType: 'text',
					success: function(text) {
						$('#' + thumb).replaceWith('<img src="' + text + '" alt="" id="' + thumb + '" />');
					}
				});
			}
		},	
		bgiframe: false,
		width: 800,
		height: 400,
		resizable: false,
		modal: false
	});
};

function markStyle(table, id, style) {
	$.getJSON('index.php?route=common/ajax/mark_style&token=' + TOKEN, {id: id, table: table, style: style}, function(rs){
	});
}

function showNoteDialog(table, id) {
	var content = jQuery.trim($('#note-' + id).text());
	var html = '<div><textarea style="width:100%;height:120px;" id="note" name="note">' + content + '</textarea></div>';
	var submit = function (v, h, f) {
		if (v == 1 && f.note == content) return true;
		
		if (v == 1) {
			content = f.note;
			$('#note-' + id).html('<img src="view/image/note.png" /><span class="none">' + content + '</span>');
		}
		else {
			if(content == '') return;
			content = '';
			$('#note-' + id).html('');
		}
		$.getJSON('index.php?route=common/ajax/add_note&token=' + TOKEN, {id: id, table: table, note: content}, function(rs){
		});
		
	    return true;
	};

	$.jBox(html, { title: "Note #" + id, buttons: {'Save': 1, 'Delete': 2}, submit: submit});
}

$(function() {
	$('#loading').ajaxSend(function() {
		$(this).show();
	});
	$('#loading').ajaxComplete(function(){
		$(this).hide();
	});
});